package com.yourname.custom;

import com.yourname.entity.Course;
import com.yourname.entity.Student;
import com.yourname.entity.Teacher;

import java.util.ArrayList;

public class CustomCourseList extends ArrayList<Course> {
    @Override
    public boolean contains(Object o) {
        Course s=(Course) o;
        for (Course course:this){
            if (course.getId().equals(s.getId())){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean add(Course course) {
        if (contains(course)){
            System.out.printf("学生%s已存在",course.getId());
            return false;
        }else{
            return super.add(course);
        }
    }
    public Course get(String sid) {
        for (Course c:this){
            if (c.getId().equals(sid)){
                return c;
            }
        }
        return null;
    }
    public boolean update(String sid,Course newCourse) {
        for (int i=0;i<this.size();i++){
            if (this.get(i).getId().equals(sid)){
                this.set(i,newCourse);
                return true;
            }
        }
        return false;
    }
    public Course set(Course newCourse){
        Course ori=null;
        for (int i=0;i<this.size();i++){
            Course target=this.get(i);
            ori=target;
            if (this.get(i).getId().equals(newCourse.getId())){
                return this.set(i,newCourse);
            }
        }
        return ori;
    }
}
